package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author ZW
 * @DATE 2020/8/2 20:40
 * @Description
 **/

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    //添加购物车
    @GetMapping("/addCart")
    public Result addCart(@RequestParam("skuId") String skuId , @RequestParam("num") Integer num){

        //动态获取登陆人信息,暂时定位静态
//        String username = "heima";
        cartService.addCart(skuId,num,tokenDecode.getUserInfo().get("username"));

        return new Result(true, StatusCode.OK,"加入购物车成功");
    }

    //查询购物车
    @GetMapping("/list")
    public Map list(){
        //动态获取登陆人信息,暂时定位静态
        //String username = "heima";
        //tokenDecode.getUserInfo().get("username")，动态获取登陆人名字
        Map map = cartService.list(tokenDecode.getUserInfo().get("username"));
        return map;
    }
}
